﻿Imports System
Imports System.Collections
Imports System.Reflection
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities
    Friend NotInheritable Class InvokeWebServiceValidator
        Inherits ActivityValidator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim activity As InvokeWebServiceActivity = [TryCast](obj,InvokeWebServiceActivity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(InvokeWebServiceActivity).FullName }), "obj")
            End If
            If (activity.ProxyClass Is Nothing) Then
                Dim item As New ValidationError(SR.GetString("Error_TypePropertyInvalid", New Object() { "ProxyClass" }), &H116) { _
                    .PropertyName = "ProxyClass" _
                }
                errors.Add(item)
                Return errors
            End If
            If (DirectCast(manager.GetService(GetType(ITypeProvider)), ITypeProvider) Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(ITypeProvider).FullName }))
            End If
            Dim proxyClass As Type = activity.ProxyClass
            If ((activity.MethodName Is Nothing) OrElse (activity.MethodName.Length = 0)) Then
                errors.Add(ValidationError.GetNotSetValidationError("MethodName"))
                Return errors
            End If
            Dim method As MethodInfo = proxyClass.GetMethod(activity.MethodName)
            If (method Is Nothing) Then
                Dim error2 As New ValidationError(SR.GetString("Error_MethodNotExists", New Object() { "MethodName", activity.MethodName }), &H137) { _
                    .PropertyName = "MethodName" _
                }
                errors.Add(error2)
                Return errors
            End If
            Dim list As New ArrayList(method.GetParameters)
            If (Not method.ReturnType Is GetType(Void)) Then
                list.Add(method.ReturnParameter)
            End If
            Dim info2 As ParameterInfo
            For Each info2 In list
                Dim name As String = info2.Name
                If (info2.Position = -1) Then
                    name = "(ReturnValue)"
                End If
                Dim binding As Object = Nothing
                If activity.ParameterBindings.Contains(name) Then
                    If activity.ParameterBindings.Item(name).IsBindingSet(WorkflowParameterBinding.ValueProperty) Then
                        binding = activity.ParameterBindings.Item(name).GetBinding(WorkflowParameterBinding.ValueProperty)
                    Else
                        binding = activity.ParameterBindings.Item(name).GetValue(WorkflowParameterBinding.ValueProperty)
                    End If
                End If
                If (Not activity.ParameterBindings.Contains(name) OrElse (binding Is Nothing)) Then
                    Dim notSetValidationError As ValidationError = ValidationError.GetNotSetValidationError(name)
                    If InvokeWebServiceActivity.ReservedParameterNames.Contains(name) Then
                        notSetValidationError.PropertyName = ParameterInfoBasedPropertyDescriptor.GetParameterPropertyName(activity.GetType, name)
                    End If
                    notSetValidationError.PropertyName = name
                    errors.Add(notSetValidationError)
                    [Continue] For
                End If
                Dim read As AccessTypes = AccessTypes.Read
                If (info2.IsOut OrElse info2.IsRetval) Then
                    read = AccessTypes.Write
                ElseIf info2.ParameterType.IsByRef Then
                    read = (read Or AccessTypes.Write)
                End If
                Dim errors2 As ValidationErrorCollection = ValidationHelpers.ValidateProperty(manager, activity, binding, New PropertyValidationContext(activity.ParameterBindings.Item(name), Nothing, name), New BindValidationContext(If(info2.ParameterType.IsByRef, info2.ParameterType.GetElementType, info2.ParameterType), read))
                If InvokeWebServiceActivity.ReservedParameterNames.Contains(name) Then
                    Dim error4 As ValidationError
                    For Each error4 In errors2
                        error4.PropertyName = ParameterInfoBasedPropertyDescriptor.GetParameterPropertyName(activity.GetType, name)
                    Next
                End If
                errors.AddRange(errors2)
            Next
            If (activity.ParameterBindings.Count > list.Count) Then
                errors.Add(New ValidationError(SR.GetString("Warning_AdditionalBindingsFound"), &H630, True))
            End If
            Return errors
        End Function

    End Class
End Namespace

